<?php
/* =========================================
 * A View is designed to take some input
 * Collect more data from the Model if necessary
 * Then render it all using a template
 * 
 * This is mainly designed for html views
 */
class Cerad_View
{
	protected $context = NULL;
		
	function __construct($context)
	{
		$this->context = $context;
	}
	function getContext() { return $this->context; }
	function getDb()      { return $this->context->getDb(); }
	
	function render($tplName)
    {
    	// Just for grins, same as the autoload function
    	$tplPath = str_replace('_', DIRECTORY_SEPARATOR, $tplName) . '.phtml';
    	        
        ob_start();
        include $tplPath;
        return ob_get_clean();
    }
	function process($data)
	{
	}
	/* -------------------------------------------
	 * Helper functions for use by templates
	 */
	public function escape($data)
	{
		return htmlspecialchars($data,ENT_COMPAT,'UTF-8');
	}
	public function formatDate($date)
    {
        if (strlen($date) < 8) return $date;
        
        $stamp = mktime(0,0,0,substr($date,4,2),substr($date,6,2),substr($date,0,4));
        
        return date('D M d',$stamp);
    }
    public function formatTime($time)
    {
        switch(substr($time,0,2)) {
            case 'BN': return 'BYE No Game';
            case 'BW': return 'BYE Want Game';
            case 'TB': return 'TBD';   
        }
        $stamp = mktime(substr($time,0,2),substr($time,2,2));
        
        return date('h:i a',$stamp);
    }
    public function formOptions($options, $value = NULL)
    {
        $html = NULL;
        foreach($options as $key => $content)
        {
            if ($key == $value) $select = ' selected="selected"';
            else                $select = NULL;
            
            $html .= 
                '<option value="' . htmlspecialchars($key) . '"' . $select .
                '>' . htmlspecialchars($content) . '</option>' . "\n";
            
        }
        return $html;
    }
    public function formCheckBox($name,$checked = FALSE, $value='1')
    {
        if ($checked) $check = 'checked="checked" ';
        else          $check = '';
        
        $value = $this->escape($value);
        
        return "<input type=\"checkbox\" name=\"{$name}\" value=\"{$value}\" {$check}\>\n";
    }
    public function formRadioBox($name,$checked = FALSE, $value='1')
    {
        if ($checked) $check = 'checked="checked" ';
        else          $check = '';
        
        $value = $this->escape($value);
        
        return "<input type=\"radio\" name=\"{$name}\" value=\"{$value}\" {$check}\>\n";
    }
    function formUDC($name,$id)
    {
        if ($id) {
            $disable = '';
            $createValue = 'Clone';
        }
        else {
            $disable = 'disabled="disabled" ';
            $createValue = 'Create';
        }
        
        $html = NULL;
        $html .= "<input type=\"checkbox\" name=\"{$name}_confirm_delete\" value=\"1\"      {$disable}/>\n";
        $html .= "<input type=\"submit\"   name=\"{$name}_submit_delete\"  value=\"Delete\" {$disable}/>\n";
        $html .= "<input type=\"submit\"   name=\"{$name}_submit_create\"  value=\"{$createValue}\" />\n";
        $html .= "<input type=\"submit\"   name=\"{$name}_submit_update\"  value=\"Update\" {$disable}/>\n";

        return $html;
    }
    /* Content is assumed to be already escaped usually by display items */   
    public function href($content,$action = NULL,$data = NULL)
    {
        $link = $this->context->getUrl()->link($action,$data);
        
        return "<a href=\"{$link}\">{$content}</a>";
    }
    public function link($action = NULL,$data = NULL)
    {
        return $this->context->getUrl()->link($action,$data);
    }
    public function file($path)
    {
        return $this->context->url->file($path);
    }
}
?>